#!/bin/bash

progname=$0

function usage () {
   cat <<EOF

Usage: $progname [-c]

    -d  - removes all data
    -c  - clean slate install, removes all files before installing the product.

EOF
   exit 1
}

#Check environment
if [ -z $DAILY_GIGGLE_ROOT ]
then
    echo "Environment variable DAILY_GIGGLE_ROOT is no set. It should be set to the root directory of the Daily Giggle intstallation"
    exit 2
fi

#Initialize vars
TMP=~/tmp/miod
DELIVERABLE=daily-giggle-trunk.tar.gz
remove_all_data=false
is_clean_slate_install=false

#Read command input
while getopts "dc" opt; do
   case $opt in
       d )  remove_all_data=true;;
       c )  is_clean_slate_install=true;;
       \?)  usage ;;
   esac
done

#Save off installation specific files
sudo cp $DELIVERABLE $DAILY_GIGGLE_ROOT/php/local_config.php $TMP

#Examine options
if [ $remove_all_data == true ]
then
    echo "Removing all data"
    mysql -u miod_user -p -D miod < $DAILY_GIGGLE_ROOT/sql/create-miod-schema.sql
fi

if [ $is_clean_slate_install == true ]
then
    echo "Removing previous installation"
    sudo rm -rf $DAILY_GIGGLE_ROOT/*
fi

#Do the install steps
sudo cp $DELIVERABLE $DAILY_GIGGLE_ROOT
cd $DAILY_GIGGLE_ROOT
sudo tar -xzf $DELIVERABLE
sudo cp $TMP/local_config.php php
cd -